﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunElements;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunProperties;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Texts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Samples.Word
{
    class Formattings
    {
        public static void Create()
        {
            Document doc = DocumentManager.Instance.CreateWordDocument();
            Body body = doc.CreateBody();

            Run r1 = body.AddParagraph().AddRun();
            r1.AddSimpleText("This is a bold paragraph");
            r1.AddProperty(new Bold());

            Run r2 = body.AddParagraph().AddRun();
            r2.AddSimpleText("This is an italic paragraph");
            r2.AddProperty(new Italics());

            Paragraph p1 = body.AddParagraph();
            Run r3 = p1.AddRun();
            //This line doesn't keep the space character
            //r3.AddSimpleText("Hello ");
            //so use this instead
            r3.AddElement(new Text()
            {
                Space = "preserve",
                Value = "Hello "
            });
            r3.AddProperty(new Bold());
            Run r4 = p1.AddRun();
            r4.AddSimpleText("world!");
            r4.AddProperty(new Italics());

            Paragraph p2 = body.AddParagraph();
            Run r5 = p2.AddRun();
            r5.AddSimpleText("This is a multiline text with breaks and without break:");
            r5.AddElement(new Break());
            r5.AddElement(new Break());
            r5.AddSimpleText(@"Lorem ipsum dolor sit amet, consectetuer adipiscing "+
                "elit. Maecenas porttitor congue massa. Fusce posuere, " +
                "magna sed pulvinar ultricies, purus lectus malesuada libero, " +
                "sit amet commodo magna eros quis urna. Nunc viverra imperdiet " +
                "enim. Fusce est. Vivamus a tellus. Pellentesque habitant " +
                "morbi tristique senectus et netus et malesuada fames ac " +
                "turpis egestas. Proin pharetra nonummy pede.");

            Paragraph p3 = body.AddParagraph();
            p3.AddRun().AddElement(new CarriageReturn());
            Run r6 = p3.AddRun();
            //note: text size measures in half-points
            r6.AddProperty(new FontSize() { Size = 30 });
            r6.AddProperty(new Fonts { AsciiFont = "Arial" });
            r6.AddSimpleText("Different font size and family");

            doc.Save(@"samples\formattings.docx");

            Document parsed = null;
            if (DocumentManager.Instance.OpenWordDocument(@"samples\formattings.docx", out parsed))
            {
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
